package cn.xsirl.websocket.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

/**
 * @version V1.0
 * @Title:
 * @ClassName: $PackageName$.$ClassName$.java
 * @Description:
 * @author: 谢明珑
 * @date: 2021/5/26$ 10:10$
 */
@Component
public class ChatRoomInterceptor implements HandshakeInterceptor {
    private Logger log = LoggerFactory.getLogger(ChatRoomInterceptor.class);
    
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        log.info("------------- 握手前拦截 -------------");
        attributes.forEach((k,v ) -> System.out.println(k + ": " + v));
        return true;
    }
    
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        log.info("------------- 握手后拦截 -------------");
    }
}
